package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.User;
import com.google.gson.Gson;
import service.ServiceLogin;


/**
 * Servlet implementation class servlet.login
 */
@WebServlet("/servlet.login")
public class login extends HttpServlet {
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public login() {
        super();
        // TODO Auto-generated constructor stub
    }
    

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();

		String number = request.getParameter("number");
		String passwd = request.getParameter("passwd");
		System.out.println(number);
		System.out.println(passwd);
		User user = new User(number, passwd);
		int result = new ServiceLogin().login(user);
		if(result == 1){
			System.out.println("登录成功");
		}else {
			System.out.println("登录失败");
		}
//		for(int i = 0; i < list.size(); i+=2)
//		{
//			if(String.format("%s", list.get(i)).equals(number) && String.format("%s", list.get(i+1)).equals(passwd))
//			{
//
//
//				Gson gson = new Gson();
//				ArrayList<Test1Object> arraylistTest = new ArrayList<Test1Object>();
//
//				Test1Object t1 = new Test1Object();
//
//				t1.setName("ture");
//
//				arraylistTest.add(t1);
//
//				String info = gson.toJson(arraylistTest);
//				// ��ӡ���z��
//				System.out.println("��ˣ�"+info);
//				// ���ؽoǰ��
//				out.write(info);
//				find = 1;
//			}
//		}
//
//		if(find == 0)
//		{
//			response.setContentType("text/html");
//			response.setCharacterEncoding("utf-8");// ����Ϊutf-8��ֹǰ���յ�������Ϊ����
//			PrintWriter out = response.getWriter();
//
//			Gson gson = new Gson();
//			ArrayList<Test1Object> arraylistTest = new ArrayList<Test1Object>();
//
//			Test1Object t1 = new Test1Object();
//
//			t1.setName("false");
//
//			arraylistTest.add(t1);
//
//			String info = gson.toJson(arraylistTest);
//			// ��ӡ���z��
//			System.out.println("��ˣ�"+info);
//			// ���ؽoǰ��
//			out.write(info);
//			//response.sendRedirect("index.html");
//		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
//	class Test1Object {
//		private String name;
//
//		public String getName() {
//			return name;
//		}
//		public void setName(String name) {
//			this.name = name;
//		}
//	}
}
